home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / read.mgbaker / read.c < prev   
Encoding:
C/C++ Source or Header  |  1990-03-21  |  2.2 KB  |  83 lines

  1. /* 
  2.  * read.c --
  3.  *
  4.  *    This program is a stand-alone benchmark to measure
  5.  *    the speed of reading a file.  It should be invoked
  6.  *    as follows:
  7.  *
  8.  *    read [count]
  9.  *
  10.  *    The program will read its standard input file count
  11.  *    times from start to finish, then print out the speed
  12.  *    of reading in bytes/sec.  If omitted, count defaults
  13.  *    to 1.
  14.  *
  15.  * Copyright 1989 Regents of the University of California.
  16.  * Permission to use, copy, modify, and distribute this
  17.  * software and its documentation for any purpose and without
  18.  * fee is hereby granted, provided that the above copyright
  19.  * notice appear in all copies.  The University of California
  20.  * makes no representations about the suitability of this
  21.  * software for any purpose.  It is provided "as is" without
  22.  * express or implied warranty.
  23.  */
  24.  
  25. #ifndef lint
  26. static char rcsid[] = "$Header: /sprite/src/benchmarks/read/RCS/read.c,v 1.6 89/09/13 20:47:51 ouster Exp $ SPRITE (Berkeley)";
  27. #endif not lint
  28.  
  29.  
  30. #include <stdio.h>
  31. #include <sys/time.h>
  32. #include <sys/resource.h>
  33.  
  34. static char buffer[16384];
  35.  
  36. main(argc, argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     int cnt, total, repeats;
  41.     double rate, micros;
  42.     struct rusage begin ,end;
  43.     struct timeval start, stop;
  44.     struct timezone tz;
  45.  
  46.     if (argc == 1) {
  47.     repeats = 1;
  48.     } else {
  49.     repeats = atoi(argv[1]);
  50.     }
  51.     total = 0;
  52.  
  53. #ifdef GETRUSAGE
  54.     getrusage(RUSAGE_SELF, &begin);
  55. #else
  56.     gettimeofday(&start, (struct timezone *) NULL);
  57. #endif
  58.  
  59.     for ( ; repeats > 0; repeats--) {
  60.         cnt = read(0, buffer, 1024);
  61.         total += cnt;
  62.         if (cnt < 1024) {
  63.         break;
  64.         }
  65.     }
  66. #ifdef GETRUSAGE
  67.     getrusage(RUSAGE_SELF, &end);
  68.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  69.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  70.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  71.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  72. #else
  73.     gettimeofday(&stop, (struct timezone *) NULL);
  74.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  75.         + stop.tv_usec - start.tv_usec;
  76. #endif
  77.     rate = total/(micros/1000000.0);
  78.     
  79.     printf("%d bytes read at %.0f bytes/sec.\n", total, rate);
  80.     printf("%d bytes read at %.0f microsec.\n", total, micros);
  81.     printf("%d bytes read at %.0f msecs.\n", total, micros / 1000);
  82. }
  83.